# $Id: cmake.sh.in 761 2008-10-09 06:23:35Z enki $
#
# buildsys/cmake.sh: CMake source package functions
#
# ---------------------------------------------------------------------------
test $lib_buildsys_cmake_sh || {

# ---------------------------------------------------------------------------
: ${prefix:="@prefix@"}
: ${exec_prefix:="@prefix@"}
: ${sysconfdir:="/etc"}
: ${libdir:="${exec_prefix}/lib"}
: ${bindir:="${exec_prefix}/bin"}
: ${shlibdir:="${libdir}/sh"}

# ---------------------------------------------------------------------------
. $shlibdir/std/array.sh

# ---------------------------------------------------------------------------
cmake_file="CMakeLists.txt CMakeCache.txt"
cmake_generated="*.cmake CMakeCache.txt CMakeFiles install_manifest.txt"

# cmake_scan [directories...]
#
# searches one or more directory trees for the CMakeLists.txt file.
# -------------------------------------------------------------------------
cmake_scan()
{
 (IFS="/ " 
  test "$#" -gt 0 || set .
  while test -n "$1"; do
    test -d "$1" && for f in $cmake_file; do
      $cmd "$1/$f" 2>/dev/null >/dev/null || continue
      (set -- $1; echo "$*"); break
    done; shift
  done)
}

# cmake_subdirs [directories...]
# -------------------------------------------------------------------------
cmake_subdirs()
{
  test -n "$*" || set . 
  while [ "$#" -gt 0 ]; do
    cmake_check "$1" &&
    sed -n \
   "/SUBDIR/ {
      s/^.*SUBDIR[A-Z]*//gi
      :lp /^\s*(/! { N; b lp; }
      s/^\s*(//
      :rp /)/! { N; b lp; }
      s/).*//
      s:^:$1/:
      p
    }" \
   "$1/CMakeLists.txt"
    shift
  done
}

# cmake_clean [directories...]
#
# Cleans files generated by CMake files in the specified directories.
# -------------------------------------------------------------------------
cmake_clean()
{
  test -n "$*" || set .
  while [ "$#" -gt 0 ]; do
    dir=$1; shift; cmake_check "$dir" || continue
    set -- `cmake_subdirs "$dir"` "$@"
   (cd "$dir"
    grep -q '^# CMAKE' Makefile 2>/dev/null && rm -f Makefile
    for file in $cmake_generated; do
#        msg Checking for $file ...
      out=$dir/$subdir/$file
      out=${out#./}
      test -f $file && echo ${out#/} && rm -f ${file#/}
      test -d $file && (cd "$file" && find * 2>/dev/null) | sed "s:^:${out#./}/:" && rm -rf $file          
    done)
  done
}

# cmake_check [ -p | --project ] [ -s | --sourcedir ] <directory>
#
# checks one or more directories for the presence of CMakeLists.txt.
# -------------------------------------------------------------------------
cmake_check()
{
 (IFS=" "
  cmd="test -f" file=$cmake_file
  case $1 in
    -p | --project) 
      set -f
      cmd="grep -E ^[^#]*[Pp][Rr][Oo][Jj][Ee][Cc][Tt][\s\t]*\\([^)]+\\)"
      shift
    ;;

    -s | --source*dir) 
      file=`set -- $file; echo $1`
      shift
    ;;
  esac
  for file in $file; do
    $cmd "$1/$file" 2>/dev/null >/dev/null && exit 0
  done
  exit 1)      
}

# cmake_values [source dirs...]
#
# for a list of cmake source trees it outputs a list of their variables set
# -------------------------------------------------------------------------
cmake_values()
{
  (for dir in "${@-.}"; do
    cmake_check "$dir" || return 1
   (cd "$dir"
    cmake -LA . 2>&1 | sed -n '/^[_A-Za-z][_0-9A-Za-z]*:[_A-Z]\+=/p')
  done)
}

# cmake_builddir [source dirs...]
#
# for a list of cmake source trees it outputs a list of their out-of-source
# build directories...
# -------------------------------------------------------------------------
cmake_builddir()
{
 (for dir; do
    cmake_check "$dir" || return 1
    parent=`dirname "$dir"` 
    base=`basename "$dir"`
    echo $parent/$base-build
  done)
}

# --- eof ---------------------------------------------------------------------
lib_buildsys_cmake_sh=:;}
